package cn.com.egova.materialdesignexample.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.com.egova.materialdesignexample.R;
import cn.com.egova.materialdesignexample.recyclerview.DividerGridItemDecoration;
import cn.com.egova.materialdesignexample.recyclerview.DividerItemDecoration;
import cn.com.egova.materialdesignexample.recyclerview.MyAdapter;
import cn.com.egova.materialdesignexample.recyclerview.RecyclerViewClickListener;
import cn.com.egova.materialdesignexample.recyclerview.RecyclerViewClickListener2;
import cn.com.egova.materialdesignexample.recyclerview.StaggeredAdapter;
import it.gmariotti.recyclerview.adapter.AlphaAnimatorAdapter;
import it.gmariotti.recyclerview.adapter.AnimatorAdapter;
import it.gmariotti.recyclerview.adapter.ScaleInAnimatorAdapter;
import it.gmariotti.recyclerview.adapter.SlideInBottomAnimatorAdapter;
import it.gmariotti.recyclerview.adapter.SlideInLeftAnimatorAdapter;
import it.gmariotti.recyclerview.adapter.SlideInRightAnimatorAdapter;
import it.gmariotti.recyclerview.itemanimator.ScaleInOutItemAnimator;
import it.gmariotti.recyclerview.itemanimator.SlideInOutBottomItemAnimator;
import it.gmariotti.recyclerview.itemanimator.SlideInOutLeftItemAnimator;
import it.gmariotti.recyclerview.itemanimator.SlideInOutRightItemAnimator;
import it.gmariotti.recyclerview.itemanimator.SlideInOutTopItemAnimator;
import it.gmariotti.recyclerview.itemanimator.SlideScaleInOutRightItemAnimator;

public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    /**RecyclerView的布局管理器*/
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter mAdapter;
    private List<String> mData;
    private RecyclerView.OnItemTouchListener mOnItemTouchListener;
    /**带有动画效果的Adapter(which work when loading on the screen or when scrolling)*/
    private AnimatorAdapter mAnimatorAdapter;
    /**条目动画(which work when itemview add,remove,change)的类型*/
    private int mItemAnimatorType=ANIMATORTYPE_SLIDEINOUTLEFT;
    public static final int ANIMATORTYPE_SLIDEINOUTLEFT=1;
    public static final int ANIMATORTYPE_SLIDEINOUTRIGHT=2;
    public static final int ANIMATORTYPE_SLIDEINOUTTOP=3;
    public static final int ANIMATORTYPE_SLIDEINOUTBOTTOM=4;
    public static final int ANIMATORTYPE_SCALEINOUT=5;
    public static final int ANIMATORTYPE_SLIDESCALEINOUTRIGHT=6;

    /**RecyclerView的条目修饰项,如分割线.*/
    private RecyclerView.ItemDecoration mItemDecoration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        initToolbar();

        initData();

        initView();
    }

    private void initView() {
        //1 实例化RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //2 为RecyclerView创建布局管理器，这里使用的是LinearLayoutManager，表示里面的Item排列是线性排列
//        mLayoutManager = new LinearLayoutManager(this);
//        mRecyclerView.setLayoutManager(mLayoutManager);
//        mAdapter = new MyAdapter(mData);
        //3 设置数据适配器
//        mRecyclerView.setAdapter(mAdapter);
        showRecyclerView(0);
    }

    private void initData() {
        mData = new ArrayList<>();
        for (int i = 0; i < 27; i++) {
            mData.add("Item" + i);
        }
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("RecyclerView");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    private void showRecyclerView(int type) {
        //RecyclerView的布局类型.
        switch (type) {
            case 0:
                mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); //最后一个参数表示是否从最后一个数据开始显示.
                mAdapter = new MyAdapter(mData);
                break;
            case 1:
                mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
                mAdapter = new MyAdapter(mData);
                break;
            case 2:
                mLayoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
                mAdapter = new MyAdapter(mData);
                break;
            case 3:
                mLayoutManager = new GridLayoutManager(this, 4, GridLayoutManager.HORIZONTAL, false);
                mAdapter = new MyAdapter(mData);
                break;
            case 4:
                mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
                mAdapter = new StaggeredAdapter(mData, 2);
                break;
            case 5:
                mLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL);
                mAdapter = new StaggeredAdapter(mData, 1);
                break;
        }

        //下面是演示处理RecyclerView点击事件的两种方法,一种是在Adapter的onBindViewHolder()进行转换,
        // 一种是通过RecyclerView自带的addOnItemTouchListener进行处理,但是需要自己写一个事件处理类,
        //本实例写了两个事件处理类,一个没有用GestureDetector,一个使用了,建议使用,否则长按事件会有个小bug,
        //即必须松开才回调,如果使用GestureDetector则没有这个bug;
        if (type < 4) {
            //移除掉之前已经存在的监听,因为本例切换布局模式时,mRecyclerView会设置各种监听,容易出现混淆和冲突.
            if(mOnItemTouchListener!=null){
                mRecyclerView.removeOnItemTouchListener(mOnItemTouchListener);
            }
            final MyAdapter adapter = (MyAdapter) mAdapter;
            adapter.setmOnItemClickListener(new MyAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Toast.makeText(RecyclerViewActivity.this, "点击了item" + position, Toast.LENGTH_SHORT).show();
                    adapter.addData(position);  //单击新增
                }
            });

            adapter.setmOnItemLongClickListener(new MyAdapter.OnItemLongClickListener() {
                @Override
                public void onItemLongClick(View view, int position) {
                    Toast.makeText(RecyclerViewActivity.this, "长按了item" + position, Toast.LENGTH_SHORT).show();
                    adapter.removeData(position);  //长按删除
                }
            });
        } else if (type < 5) {
            //移除掉之前已经存在的监听,因为本例切换布局模式时,mRecyclerView会设置各种监听,容易出现混淆和冲突.
            if(mOnItemTouchListener!=null){
                mRecyclerView.removeOnItemTouchListener(mOnItemTouchListener);
            }
            final StaggeredAdapter adapter = (StaggeredAdapter) mAdapter;
            //调用RecyclerView#addOnItemTouchListener方法能添加一个RecyclerView.OnItemTouchListener对象
            mOnItemTouchListener = new RecyclerViewClickListener(RecyclerViewActivity.this, new RecyclerViewClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Toast.makeText(RecyclerViewActivity.this, "点击了item" + position, Toast.LENGTH_SHORT).show();
                    adapter.addData(position);  //单击新增
                }

                @Override
                public void onItemLongClick(View view, int position) {
                    Toast.makeText(RecyclerViewActivity.this, "长按了item" + position, Toast.LENGTH_SHORT).show();
                    adapter.removeData(position); //长按删除
                }
            });
            mRecyclerView.addOnItemTouchListener(mOnItemTouchListener);
        } else {
            //移除掉之前已经存在的监听,因为本例切换布局模式时,mRecyclerView会添加各种监听,容易出现混淆和冲突.
            if(mOnItemTouchListener!=null){
                mRecyclerView.removeOnItemTouchListener(mOnItemTouchListener);
            }
            final StaggeredAdapter adapter = (StaggeredAdapter) mAdapter;
            mOnItemTouchListener = new RecyclerViewClickListener2(RecyclerViewActivity.this, mRecyclerView, new RecyclerViewClickListener2.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Toast.makeText(RecyclerViewActivity.this, "点击了item" + position, Toast.LENGTH_SHORT).show();
                    adapter.addData(position);  //单击新增
                }

                @Override
                public void onItemLongClick(View view, int position) {
                    Toast.makeText(RecyclerViewActivity.this, "长按了item" + position, Toast.LENGTH_SHORT).show();
                    adapter.removeData(position); //长按删除
                }
            });
            mRecyclerView.addOnItemTouchListener(mOnItemTouchListener);
        }

        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAnimatorAdapter==null?mAdapter:mAnimatorAdapter);

        //RecyclerView的item动画.
        switch (mItemAnimatorType) {
            case  ANIMATORTYPE_SLIDEINOUTLEFT:
                mRecyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(mRecyclerView));
                break;
            case  ANIMATORTYPE_SLIDEINOUTRIGHT:
                mRecyclerView.setItemAnimator(new SlideInOutRightItemAnimator(mRecyclerView));
                break;
            case  ANIMATORTYPE_SLIDEINOUTTOP:
                mRecyclerView.setItemAnimator(new SlideInOutTopItemAnimator(mRecyclerView));
                break;
            case  ANIMATORTYPE_SLIDEINOUTBOTTOM:
                mRecyclerView.setItemAnimator(new SlideInOutBottomItemAnimator(mRecyclerView));
                break;
            case  ANIMATORTYPE_SCALEINOUT:
                mRecyclerView.setItemAnimator(new ScaleInOutItemAnimator(mRecyclerView));
                break;
            case  ANIMATORTYPE_SLIDESCALEINOUTRIGHT:
                mRecyclerView.setItemAnimator(new SlideScaleInOutRightItemAnimator(mRecyclerView));
                break;
        }


        //添加分割线.注意:凡是这种addItemXXX(),这样的api,一般都是订阅模式或观察者模式,和registerListener一样的,
        //源码中一般都有一个List对ItemXXX进行维护,所以一般都会提供add,remove等方法.如果是同一种类型的ItemXXX,
        // 添加新的之前一定要移除旧的,因为源码中肯定是要遍历List集合的,会重复执行ItemXXX.
        if(type==0){
            if(mItemDecoration!=null){
                mRecyclerView.removeItemDecoration(mItemDecoration);
            }
            mItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST);
            mRecyclerView.addItemDecoration(mItemDecoration);
        }else if(type==1){
            if(mItemDecoration!=null){
                mRecyclerView.removeItemDecoration(mItemDecoration);
            }
            mItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL_LIST);
            mRecyclerView.addItemDecoration(mItemDecoration);
        }else if(type==2 || type==3){
            if(mItemDecoration!=null){
                mRecyclerView.removeItemDecoration(mItemDecoration);
            }
            mItemDecoration = new DividerGridItemDecoration(this);  //不绘制最外层边框
//            mItemDecoration = new DividerGridItemDecoration2(this,4); //绘制最外层边框
            mRecyclerView.addItemDecoration(mItemDecoration);
        }else{
            //瀑布流布局的分割线自己很难写,还是用margin来解决吧.
            if(mItemDecoration!=null){
                mRecyclerView.removeItemDecoration(mItemDecoration);
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_recyclerview_type, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int type=0;
        switch (item.getItemId()) {
            case R.id.linearlayoutmanager_vertical:
                type = 0;
                mItemAnimatorType = ANIMATORTYPE_SLIDESCALEINOUTRIGHT; //多余的一种item动画类型没地方放,就放这儿吧.
                break;
            case R.id.linearlayoutmanager_horizontal:
                type = 1;
                break;
            case R.id.gridlayoutmanager_vertical:
                type = 2;
                break;
            case R.id.gridlayoutmanager_horizontal:
                type = 3;
                break;
            case R.id.staggeredlayoutmanager_vertical:
                type = 4;
                break;
            case R.id.staggeredlayoutmanager_horizontal:
                type = 5;
                break;
            case R.id.alpha:
                mAnimatorAdapter = new AlphaAnimatorAdapter(mAdapter, mRecyclerView);
                mItemAnimatorType = ANIMATORTYPE_SLIDEINOUTLEFT;
                break;
            case R.id.slideInLeft:
                mAnimatorAdapter = new SlideInLeftAnimatorAdapter(mAdapter,mRecyclerView);
                mItemAnimatorType = ANIMATORTYPE_SLIDEINOUTRIGHT;
                break;
            case R.id.slideInRight:
                mAnimatorAdapter = new SlideInRightAnimatorAdapter(mAdapter,mRecyclerView);
                mItemAnimatorType = ANIMATORTYPE_SLIDEINOUTTOP;
                break;
            case R.id.slideInBottom:
                mAnimatorAdapter = new SlideInBottomAnimatorAdapter(mAdapter,mRecyclerView);
                mItemAnimatorType = ANIMATORTYPE_SLIDEINOUTBOTTOM;
                break;
            case R.id.scaleIn:
                mAnimatorAdapter = new ScaleInAnimatorAdapter(mAdapter,mRecyclerView);
                mItemAnimatorType = ANIMATORTYPE_SCALEINOUT;
                break;
        }

        showRecyclerView(type);

        return super.onOptionsItemSelected(item);
    }
}
